//
//  StatusCellTopView.swift
//  sinaDemo
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class StatusCellTopView: UIView {
    
    // 定义约束,记录底部的约束
    var bottomConstraint: Constraint?
    
    // 模型属性
    var status: Status? {
        didSet {
            iconImageView.sd_setImageWithURL(status?.user?.userIconUrl)
            nameLabel.text = status?.user?.name
            memberView.image = status?.user?.mbrank_image
            timeLabel.text = status?.created_at
            sourceLabel.text = status?.source
            contentLabel.attributedText = status?.attributedText
            pictureView.imageURLs = status?.imageUrls
            verified_typeImage.image = status?.user?.verified_type_image
            // 判断是否有配图
            // 卸载约束
            bottomConstraint?.uninstall()
            if let urls = status?.imageUrls where urls.count > 0 { // 有配图
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
                })
                
            }else { // 没有配图
                // 取消约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(statusCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 初始化子控件
        setUpSubViews()
    }
    
    // 初始化子控件
    private func setUpSubViews() {
        addSubview(sepView)
        addSubview(iconImageView)
        addSubview(verified_typeImage)
        addSubview(nameLabel)
        addSubview(memberView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 分割视图
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.height.equalTo(10)
        }
        
        // 头像
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(statusCellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(statusCellMargin)
            make.size.equalTo(CGSize(width: statusCellIconWH, height: statusCellIconWH))
        }
        
        // 认证图标
        verified_typeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView.snp_right)
            make.centerY.equalTo(iconImageView.snp_bottom)
            make.width.height.equalTo(statusCellMemberWH)
        }
        
        // 昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_top)
            make.left.equalTo(iconImageView.snp_right).offset(statusCellMargin)
        }
        
        // 会员标志
        memberView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(statusCellMargin)
            make.size.equalTo(CGSize(width: statusCellMemberWH, height: statusCellMemberWH))
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_right).offset(statusCellMargin)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImageView.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(statusCellMargin)
        }
        
        // 内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(iconImageView.snp_left)
            make.right.equalTo(self.snp_right).offset(-statusCellMargin)
        }
        
        // 配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
        }
        
        // 一定要设置自己的约束
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
    }
    
    // 设置约束
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
    }
    
    // MARK: 懒加载
    // 头像
    private lazy var iconImageView: UIImageView = UIImageView()
    
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(title: "周帅", color: UIColor.orangeColor(), textFont: statusCellNameFont)
    
    // 会员标志
    private lazy var memberView: UIImageView = UIImageView()
    
    // 认证类型
    private lazy var verified_typeImage: UIImageView = UIImageView()
    
    // 发布时间label
    private lazy var timeLabel: UILabel = UILabel(title: "", color: UIColor.orangeColor(), textFont: statusCellNameFont)
    
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(title: "", color: UIColor.orangeColor(), textFont: statusCellNameFont)
    
    // 内容
    private lazy var contentLabel: UILabel = UILabel(title: "", color: UIColor.darkGrayColor(), textFont: statusCellContentFont)
    
    // 配图
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    
    // 顶部的分割视图
    private lazy var sepView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(red: 239/255.0, green: 239/255.0, blue: 239/255.0, alpha: 1)
        return view
        }()
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
